package com.ggshily.pv3d.model.ms3d
{
	import __AS3__.vec.Vector;
	
	import com.ggshily.game.model.ms3d.MS3DEvent;
	import com.ggshily.game.model.ms3d.MS3DFile;
	import com.ggshily.game.model.ms3d.Ms3d_triangle;
	import com.ggshily.game.model.ms3d.Ms3d_vertex;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import org.papervision3d.core.animation.IAnimatable;
	import org.papervision3d.core.animation.IAnimationProvider;
	import org.papervision3d.core.controller.AnimationController;
	import org.papervision3d.core.geom.TriangleMesh3D;
	import org.papervision3d.core.geom.renderables.Triangle3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.NumberUV;
	import org.papervision3d.core.proto.MaterialObject3D;

	public class MS3D extends TriangleMesh3D implements IAnimatable, IAnimationProvider
	{
		internal var loader : URLLoader;
		
		private var _ms3d : MS3DFile;
		private var _currentTime : Number;
		private var _currentFrame : int;
		
		public function MS3D(url : String, ml : MaterialObject3D = null, scale : Number = 1)
		{
			super(ml, null, null, url);
			
			this.scale = scale;
			
			loader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, onLoadComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			loader.load(new URLRequest(url));
		}

		private function onLoadComplete(e : Event) : void
		{
			
			_ms3d = new MS3DFile(e.currentTarget.data);
			
			_ms3d.setFrame(_currentFrame);
			buildModel();
			
//			projectTexture("x", "z");
			
			geometry._ready = true;
			
			dispatchEvent(new MS3DEvent(MS3DEvent.LOAD_COMPLETE));
		}

		private function onIoError(e : Event) : void
		{
			throw e;
		}

		private function buildModel() : void
		{
			var vertexs : Vector.<Ms3d_vertex> = _ms3d.arrVertices;
			var v : Array = [];
			for(var i : int = 0; i < vertexs.length; i++)
			{
				var vertex : Array = _ms3d.transformVertex(vertexs[i]);
				v.push(new Vertex3D(vertex[0], vertex[1], vertex[2]));
			}
			geometry.vertices = v;
			
			var triangles : Vector.<Ms3d_triangle> = _ms3d.arrTriangles;
			var ts : Array = [];
			var t : Ms3d_triangle;
			var triangle : Triangle3D;
			for(i = 0; i < triangles.length; i++)
			{
				t = triangles[i];
				triangle = new Triangle3D(this, [v[t.vertexIndices[0]], v[t.vertexIndices[1]], v[t.vertexIndices[2]]], material);
				triangle.uv = [new NumberUV(t.s[0], t.t[0]), new NumberUV(t.s[1], t.t[1]), new NumberUV(t.s[2], t.t[2])];
				ts.push(triangle);
			}
			geometry.faces = ts; 
		}

		public function get ms3d() : MS3DFile
		{
			return _ms3d;
		}

		public function get animation() : AnimationController
		{
			return null;
		}

		public function set animation(ac : AnimationController) : void
		{
			
		}

		public function play(clip:String="all", loop:Boolean=true) : void
		{
			_currentFrame++;
			
			if(_currentFrame == _ms3d.totalFrames + 1 && loop)
			{
				_currentFrame = 0;
			}
			
			_ms3d.setFrame(_currentFrame);
			buildModel();
		}

		public function pause() : void
		{
			
		}

		public function resume(loop:Boolean=true) : void
		{
			
		}

		public function stop() : void
		{
			
		}

		public function get playing() : Boolean
		{
			return false;
		}

	}
}